﻿using System;
using System.Runtime.Serialization;

namespace Modbus
{
	[Serializable]
	public class ModbusException : Exception
	{
		private readonly ModbusError errorCode;

		#region ( Constructors )

		public ModbusException()
			: this(ModbusError.Success)
		{
		}

		public ModbusException(ModbusError errorCode)
			: this(errorCode, GetErrorMessage(errorCode))
		{
		}

		public ModbusException(string message)
			: this(ModbusError.Success, message)
		{
		}

		public ModbusException(ModbusError errorCode, string message)
			: base(message)
		{
			this.errorCode = errorCode;
		}

		public ModbusException(string message, Exception innerException)
			: base(message, innerException)
		{
			this.errorCode = ModbusError.Success;
		}

		protected ModbusException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
			this.errorCode = (ModbusError)(info.GetInt32(SR.ModbusException_ErrorCode));
		}

		#endregion

		private static string GetErrorMessage(ModbusError errorCode)
		{
			// TODO: Insert implementation.
			return String.Empty; // stub.
		}

		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);

			info.AddValue(SR.ModbusException_ErrorCode, (int)(this.errorCode), typeof(int));
		}

		public ModbusError ErrorCode
		{
			get { return this.errorCode; }
		}
	}
}
